package com.wangyi.org.service.impl;

import com.wangyi.basic.service.impl.BaseServiceImpl;
import com.wangyi.basic.util.MD5Utils;
import com.wangyi.basic.util.StrUtils;
import com.wangyi.org.domain.Employee;
import com.wangyi.org.mapper.EmployeeMapper;
import com.wangyi.org.service.IEmployeeService;
import com.wangyi.user.domain.Logininfo;
import com.wangyi.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 添加重构
     * @param employee
     */
    @Override
    public void add(Employee employee) {
        // 1.处理密码和盐值
        // 获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 获取加密之后的密码
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);

        employee.setLogininfo_id(logininfo.getId());
        super.add(employee);

        // 选了角色
        if (employee.getRole_id() != null) {
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }
    }

    /**
     * 删除重构
     * @param id
     */
    @Override
    public void del(Long id) {
        Employee employee = employeeMapper.loadById(id);
        // 删除登录信息表数据
        logininfoMapper.remove(employee.getLogininfo_id());
        // 删除中间表信息
        employeeMapper.delEmployeeRole(id);
        // 删除employee表数据
        super.del(id);
    }

    /**
     * 修改重构
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        // 1.处理密码和盐值
        // 获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 获取加密之后的密码
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        Long logininfo_id = employeeMapper.loadById(employee.getId()).getLogininfo_id();
        logininfo.setId(logininfo_id);

        logininfoMapper.update(logininfo);

        employeeMapper.delEmployeeRole(employee.getId());
        // 选了角色
        if (employee.getRole_id() != null) {
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }
        // 登录信息id和店铺信息id不能修改
        super.update(employee);
    }

    /**
     * 批量删除重构
     * @param ids
     */
    @Override
    public void patchDelete(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
        super.patchDelete(ids);
    }

    public Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();

        BeanUtils.copyProperties(employee,logininfo);
        // 当前对象是店铺管理员
        logininfo.setType(0);
        return logininfo;
    }


}
